package com.dmlll.controller;

import com.dmlll.pojo.FamilyMember;
import com.dmlll.service.FamilyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
public class FamilyController {
    @Autowired
    private FamilyService familyService;
    @RequestMapping("/submitFamilyMember")
    @ResponseBody
    public Integer submitFamilyMember(Integer[] relationArr,String[] citizenIdentificationNumberArr,String[] nameArr,String[] birthArr,
                                      Integer[] sexArr,Integer[] schoolArr,Integer[] guardAgainstArr,Integer personalId,Integer[] ids) throws ParseException {
        FamilyMember[] familyMembers = new FamilyMember[sexArr.length];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < sexArr.length; i++) {

            FamilyMember familyMember = new FamilyMember();
            familyMember.setBirth(sdf.parse(birthArr[i]));
            familyMember.setCitizenIdentificationNumber(citizenIdentificationNumberArr[i]);
            familyMember.setGuardAgainst(guardAgainstArr[i]);
            familyMember.setName(nameArr[i]);
            familyMember.setSex(sexArr[i]);
            familyMember.setSchool(schoolArr[i]);
            familyMember.setRelation(relationArr[i]);
            familyMember.setPersonalId(personalId);
            familyMember.setId(ids[i]);
            familyMembers[i]=familyMember;
        }
        return familyService.submitFamilyMember(familyMembers);
    }

    @RequestMapping("getFamilyMemberByPersonalId")
    @ResponseBody
    public List<FamilyMember> getFamilyMemberByPersonalId(Integer personalId){
        List<FamilyMember> list = familyService.getFamilyMemberByPersonalId(personalId);
        return list;
    }
}
